package cn.hn.monkey.stock;

import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_BEGIN;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_CALCUTE;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_END;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_FINISHED;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_NOT;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_PROCESS;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_SWITCH_TO_CORE;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class MainActivity extends BaseActivity {

    TextView tvMsg;
    BaseApplication.UnzipDayData unzipDayData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMsg = findViewById(R.id.display_message);

        unzipDayData = new BaseApplication.UnzipDayData(this, handler);
        new Thread(unzipDayData).start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch(msg.what) {
                case MSG_LOAD_DAYS_DATA_BEGIN:
                    tvMsg.setText("资料解压开始");
                    break;
                case MSG_LOAD_DAYS_DATA_PROCESS:
                    tvMsg.setText("资料解压中:" + (String)msg.obj);
                    break;
                case MSG_LOAD_DAYS_DATA_END:
                    tvMsg.setText("资料解压结束");
                    break;
                case MSG_LOAD_DAYS_DATA_NOT:
                    tvMsg.setText("资料不存在");
                    break;
                case MSG_LOAD_DAYS_DATA_CALCUTE:
                    tvMsg.setText(msg.obj == null?"资料计算中":(String)msg.obj);
                    break;
                case MSG_LOAD_DAYS_DATA_FINISHED:
                    tvMsg.setText("资料处理完成, 稍后切换界面");
                    sendEmptyMessageDelayed(MSG_SWITCH_TO_CORE, 1000);
                    break;
                case MSG_SWITCH_TO_CORE:
                    SwitchToCore();
                    break;
            }
        }
    };

    void SwitchToCore() {
        Intent intent = new Intent(this, CoreActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("MainActivity", "onDestroy");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        unzipDayData.destroy();
    }
}